package com.situ.studentmanagement.controller;

import com.octo.captcha.Captcha;
import com.situ.studentmanagement.Constants;
import com.situ.studentmanagement.dao.*;
import com.situ.studentmanagement.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.management.MalformedObjectNameException;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import static com.situ.studentmanagement.Constants.USER_ID;

/**
 * Created by Administrator on 2016/8/3.
 */
@Controller
@RequestMapping("/Manager")
public class ManagerAccountController {
    @Autowired
    public SuperManagerMapper supermanagerMapper;

    @Autowired
    public UserMapper userMapper;

    @Autowired
    public ManageMapper manageMapper;
    @Autowired
    public OrderMapper orderMapper;

    @Autowired
    GoodsMapper goodsMapper;

    private int modifyStaffID;
    private int modifyUserID;
    private int modifyGoodID;
    private int modifyOrderID;

    //保存数组长度，下一页功能
    private int goodLenth;
    private int userLenth;

    //导航到管理注册页面
    @RequestMapping(value = "/mansigup")
    public String mansig(){

        return "manregister";
    }

    //管理员登录成功导航到管理页面
    @RequestMapping(value = "/mancheck")
    public String gotoman(Model model,HttpSession session,
                          @RequestParam("name")String name,
                          @RequestParam("password")String password,
                          @RequestParam("random")String random
                          ){

        String errMsg="";
        int logID;
        Captcha captcha = (Captcha) session.getAttribute("captcha");
        if (!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            model.addAttribute("value_name", name);
            model.addAttribute("value_password", password);
            model.addAttribute("errMsg", errMsg);
            return "manager"; //返回前台文件件的名字。
        }

            SuperManager s = supermanagerMapper.manLogin(name, password);
        if(s!=null) {
//            session.setAttribute(Constants.SUPER_Manager_ID, s.getId());
//            session.setAttribute("isSuper",1);
//            int isSuperID=Integer.parseInt(session.getAttribute("isSuper").toString());
            List<Orderone> orderones=orderMapper.selectAllOrderOne();
            model.addAttribute("orderones",orderones);
//            model.addAttribute("isSuper",isSuperID);
            return "management";
        }else if(manageMapper.mLogin(name,password)!=null) {
            List<Orderone> orderones=orderMapper.selectAllOrderOne();
            model.addAttribute("orderones",orderones);
              return "copymanagement";
        }else {

            errMsg = "用户名或密码不存在";
            model.addAttribute("errMsg", errMsg);
            model.addAttribute("value_name", name);
            model.addAttribute("value_password", password);
            return "manager"; //返回前台文件件的名字
        }

    }

    //导航到订单管理页面
    @RequestMapping("/main")
    public  String home(HttpSession session, Model model) {

        List<Orderone> orderones=orderMapper.selectAllOrderOne();
        model.addAttribute("orderones",orderones);
        return  "management";
    }




    //管理员注册成功导航到登录页面
    @RequestMapping(value = "/manlog")
    public String manlog(Model model, HttpSession session,
                         @RequestParam("name")String name,
                         @RequestParam("relname")String relname,
                         @RequestParam("password")String password,
                         @RequestParam("random")String random){
        String errMsg=null;
        Captcha captcha= (Captcha) session.getAttribute("captcha");
        if(!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            model.addAttribute("errMsg", errMsg);
            return "manregister"; //返回前台文件件的名字。
        }

        SuperManager superManager = new SuperManager(0, name, relname, password);
        int logID = supermanagerMapper.sumanRegister(superManager);
        session.setAttribute(Constants.SUPER_Manager_ID, superManager.getId());
        session.setAttribute("value_name",name);

        return "manager";

    }

    //退出登录
    @RequestMapping(value = "/back")
    public String tuichu(Model model, HttpSession session) {
        session.setAttribute(Constants.SUPER_Manager_ID, null);
        session.setAttribute(Constants.USER_ID, null);
        session.setAttribute(Constants.MANAGER_ID, null);
        session.setAttribute(Constants.MANAGER_NAME,null);
        session.setAttribute(Constants.SUPER_Manager_NAME,null);
        session.setAttribute(Constants.USER_NAME,null);
        return "manager";
    }


    //导航到员工管理页面
    @RequestMapping(value = "/man")
    public String staff(Model model, HttpSession session) {
        List<Manage> manages=manageMapper.getStaffs();
        model.addAttribute("manages",manages);
        session.setAttribute("isSuper",1);
        int isSuperID=Integer.parseInt(session.getAttribute("isSuper").toString());
        model.addAttribute("isSuper",isSuperID);
        return "staffman";
    }

    //导航到添加员工页面
    @RequestMapping(value = "/addstaff")
    public String addstaff(Model model, HttpSession session) {

        return "addstaff";
    }

    //添加员工完成，导航至员工管理页面
    @RequestMapping(value = "/staffman")
    public String staffman(Model model, HttpSession session,
                           @RequestParam("name")String name,
                           @RequestParam("realname")String realname,
                           @RequestParam("password")String password,
                           @RequestParam("passwordtwo")String passwordtwo,
                           @RequestParam("phone")String phone,
                           @RequestParam("sex")String sex,
                           @RequestParam("address")String address,
                           @RequestParam("random")String random) {
    Integer manageSex=1;
    String errMsg="";

        Captcha captcha= (Captcha) session.getAttribute("captcha");
        if(!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            model.addAttribute("errMsg", errMsg);
            return "addstaff"; //返回前台文件件的名字。
        }
        if(password.equals(passwordtwo)){
            if(sex.equals("man")){
                manageSex=1;
            }else {
                manageSex=0;
            }
            Manage manage=new Manage(0,name,realname,password,phone,manageSex,address);
            int manage_id=manageMapper.addmanager(manage);
            session.setAttribute(Constants.MANAGER_ID,manage_id);
        }else {
            model.addAttribute("errMsg","两次输入的密码不一致，请重新输入");
            return "register";
        }
        List<Manage> manages=manageMapper.getStaffs();
        model.addAttribute("manages",manages);

        return "staffman";
    }

    //用户管理页面
    //此页面获取用户信息
    @RequestMapping("/user")
    public String userman(HttpSession session, Model model,
                          @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index ) {
        //从数据库获取列表，一次获取一页，一页固定是10行
        int rowsOfPage=7;
        //获取到的列表以名字列进行排序

        List<User>user=userMapper.getUsers(page_start_index,rowsOfPage,"name");
        model.addAttribute("users",user);
        session.setAttribute("isSuper",1);
        int isSuperID=Integer.parseInt(session.getAttribute("isSuper").toString());
        model.addAttribute("isSuper",isSuperID);
        //下一页翻页功能
        List<User>users=userMapper.getAllUsers();
        userLenth=users.size();
        model.addAttribute("Lenth",userLenth);
        //“上一页”和“下一页”两个链接的参数
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        session.setAttribute("isSuper",1);

        model.addAttribute("isSuper",isSuperID);
        return  "userman";
    }

    //导航到添加用户页面
    @RequestMapping(value = "/adduser")
    public String adduse(Model model, HttpSession session) {
        return "adduser";
    }

    //添加用户成功返回用户管理页面addUserOk
    @RequestMapping(value = "/addUserOk")
    public String addUserOk(Model model, HttpSession session,
                            @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                            @RequestParam("name")String name,
                           @RequestParam("realname")String realname,
                           @RequestParam("password") String password,
                           @RequestParam("passwordtwo") String passwordtwo,
                           @RequestParam("protect") String protect,
                           @RequestParam("answer") String answer,
                           @RequestParam("phone") String phone,
                           @RequestParam("address") String address,
                           @RequestParam("sex") String sex,
                           @RequestParam("random") String random){

        Integer StudentSex=1;
        String protectstring="";
        String errMsg=null;
        Captcha captcha= (Captcha) session.getAttribute("captcha");
        if(!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            model.addAttribute("errMsg", errMsg);
            return "adduser"; //返回前台文件件的名字。
        }

        if(password.equals(passwordtwo)){
            if(sex.equals("man")){
                StudentSex=1;
            }else {
                StudentSex=0;
            }
            int protectint=Integer.parseInt(protect);
            switch (protectint){
                case 1:
                    protectstring="您父亲的名字";
                    break;
                case 2:
                    protectstring="您母亲的名字";
                    break;
                case 3:
                    protectstring="您的小学学校的名字";
                    break;
                case 4:
                    protectstring="您初中学校的名字";
                    break;
                case 5:
                    protectstring="您最好的朋友的名字";
                    break;
            }
            User user=new User(0,name,realname,password,protectstring,answer,StudentSex,phone,address);
            userMapper.insertUser(user);
            int stuID=user.getId();
            session.setAttribute(USER_ID,Integer.toString(stuID));
            model.addAttribute("value_name",name);
        }else {
            model.addAttribute("errMsg","两次输入的密码不一致，请重新输入");
            return "adduser";
        }

        int rowsOfPage=7;
        //获取到的列表以名字列进行排序
        List<User>user=userMapper.getUsers(page_start_index,rowsOfPage,"name");
        model.addAttribute("users",user);
        //下一页翻页功能
        List<User>users=userMapper.getAllUsers();
        userLenth=users.size();
        model.addAttribute("Lenth",userLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "userman";
    }


    //导航到员工信息修改页面
    @RequestMapping(value = "/modifyStaff")
    public String stamodifyStaffff(Model model, HttpSession session,
                                   @RequestParam("modify_id")int modify_id) {
        Manage manages=manageMapper.getStaffsById(modify_id);
        model.addAttribute("manages",manages);
        modifyStaffID=modify_id;
        return "modifystaff";
    }

    //修改员工信息成功，返回员工管理页面
    @RequestMapping(value = "/updateStaff")
    public String updateStaff(Model model, HttpSession session,
                              @RequestParam("name")String name,
                              @RequestParam("realname")String realname,
                              @RequestParam("password")String password,
                              @RequestParam("passwordtwo")String passwordtwo,
                              @RequestParam("phone")String phone,
                              @RequestParam("sex")String sex,
                              @RequestParam("address")String address,
                              @RequestParam("random")String random) {
        Integer manageSex = 1;
        String errMsg = "";

        Captcha captcha = (Captcha) session.getAttribute("captcha");
        if (!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            model.addAttribute("errMsg", errMsg);
            Manage manages=new Manage(0,name,realname,password,phone,manageSex,address);
            model.addAttribute("manages",manages);
            return "modifystaff"; //返回前台文件件的名字。
        }
        if (password.equals(passwordtwo)) {
            if (sex.equals("man")) {
                manageSex = 1;
            } else {
                manageSex = 0;
            }
            manageMapper.setStaffsById(modifyStaffID, name, realname, password, phone, manageSex, address);

        } else {
            model.addAttribute("errMsg", "两次输入的密码不一致，请重新输入");
            Manage manages=new Manage(0,name,realname,password,phone,manageSex,address);
            model.addAttribute("manages",manages);
            return "modifystaff";
        }
        List<Manage> manages = manageMapper.getStaffs();
        model.addAttribute("manages", manages);

        return "staffman";
    }

    //导航到用户修改页面
    @RequestMapping(value = "/modifyUser")
    public String modifyUser(Model model, HttpSession session,
                             @RequestParam("modifyuserid")int modify_id) {

        User user =userMapper.getUserById(modify_id);
        model.addAttribute("user",user);
        modifyUserID=modify_id;
        return "modifyuser";
    }
    //修改用户信息成功，返回用户管理页面
    @RequestMapping(value = "/updateUser")
    public String updateUser(Model model, HttpSession session,
                             @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                             @RequestParam("name")String name,
                              @RequestParam("realname")String realname,
                              @RequestParam("password")String password,
                              @RequestParam("passwordtwo")String passwordtwo,
                              @RequestParam("phone")String phone,
                              @RequestParam("sex")String sex,
                              @RequestParam("address")String address,
                              @RequestParam("random")String random) {
        Integer manageSex = 1;
        String errMsg = "";

        Captcha captcha = (Captcha) session.getAttribute("captcha");
        if (!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            User user=new User(0,name,realname,password,manageSex,phone,address);
            model.addAttribute("user",user);
            model.addAttribute("errMsg", errMsg);
            return "modifyuser"; //返回前台文件件的名字。
        }
        if (password.equals(passwordtwo)) {
            if (sex.equals("man")) {
                manageSex = 1;
            } else {
                manageSex = 0;
            }
            userMapper.setUserById(modifyUserID, name, realname, password, phone, manageSex, address);

        } else {
            model.addAttribute("errMsg", "两次输入的密码不一致，请重新输入");
            User user=new User(0,name,realname,password,manageSex,phone,address);
            model.addAttribute("user",user);
            return "modifyuser";
        }

        int rowsOfPage=7;
        //获取到的列表以名字列进行排序
        List<User>user=userMapper.getUsers(page_start_index,rowsOfPage,"name");
        model.addAttribute("users",user);
        //下一页翻页功能
        List<User>users=userMapper.getAllUsers();
        userLenth=users.size();
        model.addAttribute("Lenth",userLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "userman";
    }


    //导航到菜品管理页面
    @RequestMapping(value = "/food")
    public String food(Model model, HttpSession session,
                       @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index ) {

        //从数据库获取列表，一次获取一页，一页固定是10行
        int rowsOfPage=7;
        //获取到的列表以名字列进行排序
        //查询说有的菜品
        session.setAttribute("isSuper",1);
        int isSuperID=Integer.parseInt(session.getAttribute("isSuper").toString());
        model.addAttribute("isSuper",isSuperID);
        //查询说有的菜品
        List<Goods> goodses=goodsMapper.getAllGoods(page_start_index,rowsOfPage,"name");
        model.addAttribute("goods",goodses);
        //下一页翻页功能
        List<Goods> goodds=goodsMapper.getAllGoodsok();
        goodLenth=goodds.size();
        model.addAttribute("Lenth",goodLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);

        return "goodsman";
    }


  //跳转到菜品修改页面
  @RequestMapping(value = "/modifyGood")
  public String modifyGood(Model model, HttpSession session,
                           @RequestParam("modifyGoodid")int modify_id) {

      Goods goods=goodsMapper.getGoodById(modify_id);
      model.addAttribute("goods",goods);
      session.setAttribute(Constants.GOOD_ID,modify_id);
      modifyGoodID=modify_id;
      return "modifygood";
  }
    //修改菜品信息完毕返回菜品管理页面
    @RequestMapping(value = "/updateGood")
    public String updateGood(Model model,
                             HttpSession session,
                             @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                             @RequestParam("name")String name,
                             @RequestParam("price")String price,
                             @RequestParam("isfree")String isfree,
                             @RequestParam("freeprice")String freeprice){
        Integer Isfree=1;
        String errMsg = "";

                if (isfree.equals("yes")) {
                    Isfree = 1;
            } else {
                    Isfree = 0;
            }

            if(freeprice.equals("")){
                freeprice=null;
                goodsMapper.setGoodById(modifyGoodID,name,Integer.parseInt(price) ,Isfree,0);
            }
        else {
                goodsMapper.setGoodById(modifyGoodID, name, Integer.parseInt(price), Isfree, Integer.parseInt(freeprice));
            }
        //查询说有的菜品
        int rowsOfPage=7;
        List<Goods> goodses=goodsMapper.getAllGoods(page_start_index,rowsOfPage,"name");
        model.addAttribute("goods",goodses);

        //下一页翻页功能
        List<Goods> goodds=goodsMapper.getAllGoodsok();
        goodLenth=goodds.size();
        model.addAttribute("Lenth",goodLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "goodsman";

    }

//跳转到修改图片页面
    @RequestMapping("/changeimg")
    public String change( Model model, HttpSession session,@RequestParam("modifyGoodid")int modify_id){

        Goods goods=goodsMapper.getGoodById(modify_id);
        model.addAttribute("goods",goods);
        session.setAttribute("modifyGoodid",modify_id);
        return "modifygoodimg";
    }
    //修改菜品图片
    //1 实现提交文件的form
    //2 实现响应文件提交form action的方法
    //3 把文件内容保存到本地文件中，把数据库中student的头像文件名更新
    @RequestMapping(value = "/upGoodImg")
    public String uploadHeadImg(Model model,
                                HttpSession session,
                                @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                                @RequestParam("goodImg") MultipartFile file){
        String errMsg=null;
        int rowsOfPage=7;
        //将上传的文件内容保存下来
        if (!file.isEmpty()) {
            try {
            int id= (int) session.getAttribute("modifyGoodid");
                byte[] bytes = file.getBytes();
                String path = session.getServletContext().getRealPath("/WEB-INF/img");
                String file_name = "foodimg/"+String.valueOf(id)+".png";
                BufferedOutputStream stream =new BufferedOutputStream(
                        new FileOutputStream(new File(path+"/"+file_name)));
                stream.write(bytes);
                stream.close();
                //把图片文件名设置到数据库中
                goodsMapper.setGoodImgById(id,file_name);
            } catch (Exception e) {
                errMsg = e.getLocalizedMessage();
            }
        }
        model.addAttribute("errMsg",errMsg);
        //查询说有的菜品
        List<Goods> goodses=goodsMapper.getAllGoods(page_start_index,rowsOfPage,"name");
        model.addAttribute("goods",goodses);
        //下一页翻页功能
        List<Goods> goodds=goodsMapper.getAllGoodsok();
        goodLenth=goodds.size();
        model.addAttribute("Lenth",goodLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);

        return "goodsman";
    }

    //导航到添加菜品de页面
    @RequestMapping("/addGood")
    public String addGood(){
        return "addGood";
    }
    //添加菜品成功，返回菜品管理页面
    @RequestMapping("/addGoodOk")
    public String addGoodOk(Model model,HttpSession session,
                            @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                            @RequestParam("name")String name,
                            @RequestParam("price")String price){

        Goods goods=new Goods(0,name,null,Integer.parseInt(price),null,null,null,null,null);
        goodsMapper.addGoods(goods);

        //查询说有的菜品
        int rowsOfPage=7;
        List<Goods> goodses=goodsMapper.getAllGoods(page_start_index,rowsOfPage,"name");
        model.addAttribute("goods",goodses);
        int glenth=goodses.size();
        model.addAttribute("GoodLenth",glenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "goodsman";
    }


    //===========================================q权限配置问题===============================================
    //用户管理页面
    //此页面获取用户信息
    @RequestMapping("/copyuser")
    public String copyuserman(HttpSession session, Model model,
                          @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index ) {
        //从数据库获取列表，一次获取一页，一页固定是10行
        int rowsOfPage=7;
        //获取到的列表以名字列进行排序

        List<User>user=userMapper.getUsers(page_start_index,rowsOfPage,"name");
        model.addAttribute("users",user);
        session.setAttribute("isSuper",1);
        int isSuperID=Integer.parseInt(session.getAttribute("isSuper").toString());
        model.addAttribute("isSuper",isSuperID);
        //下一页翻页功能
        List<User>users=userMapper.getAllUsers();
        userLenth=users.size();
        model.addAttribute("Lenth",userLenth);
        //“上一页”和“下一页”两个链接的参数
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        session.setAttribute("isSuper",1);

        model.addAttribute("isSuper",isSuperID);
        return  "copyuserman";
    }

    //导航到添加用户页面
    @RequestMapping(value = "/copyadduser")
    public String copyadduse(Model model, HttpSession session) {
        return "copyadduser";
    }

    //添加用户成功返回用户管理页面addUserOk
    @RequestMapping(value = "/copyaddUserOk")
    public String copyaddUserOk(Model model, HttpSession session,
                            @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                            @RequestParam("name")String name,
                            @RequestParam("realname")String realname,
                            @RequestParam("password") String password,
                            @RequestParam("passwordtwo") String passwordtwo,
                            @RequestParam("protect") String protect,
                            @RequestParam("answer") String answer,
                            @RequestParam("phone") String phone,
                            @RequestParam("address") String address,
                            @RequestParam("sex") String sex,
                            @RequestParam("random") String random){

        Integer StudentSex=1;
        String protectstring="";
        String errMsg=null;
        Captcha captcha= (Captcha) session.getAttribute("captcha");
        if(!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            model.addAttribute("errMsg", errMsg);
            return "adduser"; //返回前台文件件的名字。
        }

        if(password.equals(passwordtwo)){
            if(sex.equals("man")){
                StudentSex=1;
            }else {
                StudentSex=0;
            }
            int protectint=Integer.parseInt(protect);
            switch (protectint){
                case 1:
                    protectstring="您父亲的名字";
                    break;
                case 2:
                    protectstring="您母亲的名字";
                    break;
                case 3:
                    protectstring="您的小学学校的名字";
                    break;
                case 4:
                    protectstring="您初中学校的名字";
                    break;
                case 5:
                    protectstring="您最好的朋友的名字";
                    break;
            }
            User user=new User(0,name,realname,password,protectstring,answer,StudentSex,phone,address);
            userMapper.insertUser(user);
            int stuID=user.getId();
            session.setAttribute(USER_ID,Integer.toString(stuID));
            model.addAttribute("value_name",name);
        }else {
            model.addAttribute("errMsg","两次输入的密码不一致，请重新输入");
            return "copyadduser";
        }

        int rowsOfPage=7;
        //获取到的列表以名字列进行排序
        List<User>user=userMapper.getUsers(page_start_index,rowsOfPage,"name");
        model.addAttribute("users",user);
        //下一页翻页功能
        List<User>users=userMapper.getAllUsers();
        userLenth=users.size();
        model.addAttribute("Lenth",userLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "copyuserman";
    }

    //导航到用户修改页面
    @RequestMapping(value = "/copymodifyUser")
    public String copymodifyUser(Model model, HttpSession session,
                             @RequestParam("modifyuserid")int modify_id) {

        User user =userMapper.getUserById(modify_id);
        model.addAttribute("user",user);
        modifyUserID=modify_id;
        return "copymodifyuser";
    }
    //修改用户信息成功，返回用户管理页面
    @RequestMapping(value = "/copyupdateUser")
    public String copyupdateUser(Model model, HttpSession session,
                             @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                             @RequestParam("name")String name,
                             @RequestParam("realname")String realname,
                             @RequestParam("password")String password,
                             @RequestParam("passwordtwo")String passwordtwo,
                             @RequestParam("phone")String phone,
                             @RequestParam("sex")String sex,
                             @RequestParam("address")String address,
                             @RequestParam("random")String random) {
        Integer manageSex = 1;
        String errMsg = "";

        Captcha captcha = (Captcha) session.getAttribute("captcha");
        if (!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            User user=new User(0,name,realname,password,manageSex,phone,address);
            model.addAttribute("user",user);
            model.addAttribute("errMsg", errMsg);
            return "copymodifyuser"; //返回前台文件件的名字。
        }
        if (password.equals(passwordtwo)) {
            if (sex.equals("man")) {
                manageSex = 1;
            } else {
                manageSex = 0;
            }
            userMapper.setUserById(modifyUserID, name, realname, password, phone, manageSex, address);

        } else {
            model.addAttribute("errMsg", "两次输入的密码不一致，请重新输入");
            User user=new User(0,name,realname,password,manageSex,phone,address);
            model.addAttribute("user",user);
            return "copymodifyuser";
        }

        int rowsOfPage=7;
        //获取到的列表以名字列进行排序
        List<User>user=userMapper.getUsers(page_start_index,rowsOfPage,"name");
        model.addAttribute("users",user);
        //下一页翻页功能
        List<User>users=userMapper.getAllUsers();
        userLenth=users.size();
        model.addAttribute("Lenth",userLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "copyuserman";
    }

    //导航到订单管理页面
    @RequestMapping("/copymain")
    public  String copyhome(HttpSession session, Model model) {

        List<Orderone> orderones=orderMapper.selectAllOrderOne();
        model.addAttribute("orderones",orderones);
        return  "copymanagement";
    }

    //导航到菜品管理页面
    @RequestMapping(value = "/copyfood")
    public String copyfood(Model model, HttpSession session,
                       @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index ) {

        //从数据库获取列表，一次获取一页，一页固定是10行
        int rowsOfPage=7;
        //获取到的列表以名字列进行排序
        //查询说有的菜品
        session.setAttribute("isSuper",1);
        int isSuperID=Integer.parseInt(session.getAttribute("isSuper").toString());
        model.addAttribute("isSuper",isSuperID);
        //查询说有的菜品
        List<Goods> goodses=goodsMapper.getAllGoods(page_start_index,rowsOfPage,"name");
        model.addAttribute("goods",goodses);
        //下一页翻页功能
        List<Goods> goodds=goodsMapper.getAllGoodsok();
        goodLenth=goodds.size();
        model.addAttribute("Lenth",goodLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);

        return "copygoodsman";
    }


    //跳转到菜品修改页面
    @RequestMapping(value = "/copymodifyGood")
    public String copymodifyGood(Model model, HttpSession session,
                             @RequestParam("modifyGoodid")int modify_id) {

        Goods goods=goodsMapper.getGoodById(modify_id);
        model.addAttribute("goods",goods);
        session.setAttribute(Constants.GOOD_ID,modify_id);
        modifyGoodID=modify_id;
        return "copymodifygood";
    }
    //修改菜品信息完毕返回菜品管理页面
    @RequestMapping(value = "/copyupdateGood")
    public String copyupdateGood(Model model,
                             HttpSession session,
                             @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                             @RequestParam("name")String name,
                             @RequestParam("price")String price,
                             @RequestParam("isfree")String isfree,
                             @RequestParam("freeprice")String freeprice){
        Integer Isfree=1;
        String errMsg = "";

        if (isfree.equals("yes")) {
            Isfree = 1;
        } else {
            Isfree = 0;
        }

        if(freeprice.equals("")){
            freeprice=null;
            goodsMapper.setGoodById(modifyGoodID,name,Integer.parseInt(price) ,Isfree,0);
        }
        else {
            goodsMapper.setGoodById(modifyGoodID, name, Integer.parseInt(price), Isfree, Integer.parseInt(freeprice));
        }
        //查询说有的菜品
        int rowsOfPage=7;
        List<Goods> goodses=goodsMapper.getAllGoods(page_start_index,rowsOfPage,"name");
        model.addAttribute("goods",goodses);

        //下一页翻页功能
        List<Goods> goodds=goodsMapper.getAllGoodsok();
        goodLenth=goodds.size();
        model.addAttribute("Lenth",goodLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "copygoodsman";

    }

    //跳转到修改图片页面
    @RequestMapping("/copychangeimg")
    public String copychange( Model model, HttpSession session,@RequestParam("modifyGoodid")int modify_id){

        Goods goods=goodsMapper.getGoodById(modify_id);
        model.addAttribute("goods",goods);
        session.setAttribute("modifyGoodid",modify_id);
        return "copymodifygoodimg";
    }
    //修改菜品图片
    //1 实现提交文件的form
    //2 实现响应文件提交form action的方法
    //3 把文件内容保存到本地文件中，把数据库中student的头像文件名更新
    @RequestMapping(value = "/copyupGoodImg")
    public String copyuploadHeadImg(Model model,
                                HttpSession session,
                                @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                                @RequestParam("goodImg") MultipartFile file){
        String errMsg=null;
        int rowsOfPage=7;
        //将上传的文件内容保存下来
        if (!file.isEmpty()) {
            try {
                int id= (int) session.getAttribute("modifyGoodid");
                byte[] bytes = file.getBytes();
                String path = session.getServletContext().getRealPath("/WEB-INF/img");
                String file_name = "foodimg/"+String.valueOf(id)+".png";
                BufferedOutputStream stream =new BufferedOutputStream(
                        new FileOutputStream(new File(path+"/"+file_name)));
                stream.write(bytes);
                stream.close();
                //把图片文件名设置到数据库中
                goodsMapper.setGoodImgById(id,file_name);
            } catch (Exception e) {
                errMsg = e.getLocalizedMessage();
            }
        }
        model.addAttribute("errMsg",errMsg);
        //查询说有的菜品
        List<Goods> goodses=goodsMapper.getAllGoods(page_start_index,rowsOfPage,"name");
        model.addAttribute("goods",goodses);
        //下一页翻页功能
        List<Goods> goodds=goodsMapper.getAllGoodsok();
        goodLenth=goodds.size();
        model.addAttribute("Lenth",goodLenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);

        return "copygoodsman";
    }

    //导航到添加菜品de页面
    @RequestMapping("/copyaddGood")
    public String copyaddGood(){
        return "copyaddGood";
    }
    //添加菜品成功，返回菜品管理页面
    @RequestMapping("/copyaddGoodOk")
    public String copyaddGoodOk(Model model,HttpSession session,
                            @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index,
                            @RequestParam("name")String name,
                            @RequestParam("price")String price){

        Goods goods=new Goods(0,name,null,Integer.parseInt(price),null,null,null,null,null);
        goodsMapper.addGoods(goods);

        //查询说有的菜品
        int rowsOfPage=7;
        List<Goods> goodses=goodsMapper.getAllGoods(page_start_index,rowsOfPage,"name");
        model.addAttribute("goods",goodses);
        int glenth=goodses.size();
        model.addAttribute("GoodLenth",glenth);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "copygoodsman";
    }
}
